﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

namespace ReportSystem_XY.Utility
{
    public static class XmlHelper
    {
        public static List<string> ReadXml(string nodeName)
        {
            List<string> strnodes = new List<string>();
            string path = Application.StartupPath + "\\XMLFileConfig.xml";

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(path);
            //根节点root
            XmlNode rootNode = xDoc.SelectSingleNode("root");
            string name = rootNode.Name;
            if (rootNode != null)
            {
                //获取根节点下面的子节点
                XmlNodeList nodeList = rootNode.ChildNodes;
                //遍历子节点，查找名称的子节点
                foreach (XmlNode item in nodeList)
                {
                    if (item.Name == nodeName)
                    {
                        //获取孙子节点
                        XmlNodeList subNodeList = item.ChildNodes;
                        if (subNodeList != null)
                        {
                            foreach (XmlNode xn in subNodeList)
                            {
                                XmlElement xmle = (XmlElement)xn;
                                strnodes.Add(xmle.InnerText);
                            }
                        }
                        return strnodes;
                    }
                }
            }
            return null;
        }

        public static string ReadXml(string nodeName,string attribute)
        {
            
            string path = Application.StartupPath + "\\XMLFileConfig.xml";

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(path);
            //根节点root
            XmlNode rootNode = xDoc.SelectSingleNode("root");
            string name = rootNode.Name;
            if (rootNode != null)
            {
                //获取根节点下面的子节点
                XmlNodeList nodeList = rootNode.ChildNodes;
                //遍历子节点，查找名称的子节点
                foreach (XmlNode item in nodeList)
                {
                    if (item.Name == nodeName)
                    {
                        //获取孙子节点
                        XmlNodeList subNodeList = item.ChildNodes;
                        if (subNodeList != null)
                        {
                            foreach (XmlNode xn in subNodeList)
                            {
                                if(xn.Name==attribute)
                                {
                                    XmlElement xmle = (XmlElement)xn;
                                    return (xmle.InnerText);//相同情况下的第一个
                                }                               
                            }
                        }
                    }
                }
            }
            return null;
        }
    }
}
